package com.ch.mafia.process;

import org.json.JSONObject;

import cn.goso.utility.NumberTools;

import com.ch.mafia.base.BaseProcess;

public class HouseInfoProcess extends BaseProcess{

	public boolean isStarted(String houseId) throws Exception{
		boolean result = true;
		JSONObject jsonObject = baseDao.queryBean("s_house", null,"id = "+houseId+" and state='-1'" , null);
		if(jsonObject!=null){
			result =false;
		}
		return result;
	}
	
	public boolean isFull(String houseId) throws Exception{
		boolean result = false;
		int maxNum =getHouseMaxNum(houseId);
		int nowNum = getHouseNowNum(houseId);
		if(nowNum>=maxNum){
			result =true;
		}
		return result;
	}
	
	public int getHouseMaxNum(String houseId)throws Exception{
		JSONObject jsonObject = baseDao.queryBean("s_house", new String[]{"maxNum"},"id = "+houseId, null);
		return jsonObject.getInt("maxNum");
	}
	
	public int getHouseNowNum(String houseId)throws Exception{
		JSONObject jsonPlayer = baseDao.queryBean("s_player", new String[]{"count(*) as nowNum"},"houseid = "+houseId, null);
		return jsonPlayer.getInt("nowNum");
	}
	
	public void addHouseNowNum(String houseId)throws Exception{
		baseDao.update("s_house",  new String[]{"nowNum"} ,new String[]{ "nowNum +1"}, "id="+houseId, null);
	}
	
	public void subHouseNowNum(String houseId)throws Exception{
		baseDao.update("s_house",  new String[]{"nowNum"} ,new String[]{ "nowNum -1"}, "id="+houseId, null);
	}
	
	public int getNowSeat(String houseId)throws Exception{
		int result = 0;
		JSONObject jsonObject = baseDao.queryBean("s_house", new String[]{"queue"}, "id="+houseId, null);
		String queue =jsonObject.getString("queue");
		if(queue!=null && !queue.equals("")){
			result =NumberTools.stringToInt(queue.split(",")[0].replace("{","").replace("}",""),0);
		}
		return result;
	}
}
